package com.loadmore.oneday.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.loadmore.common.base.BaseAdapter
import com.loadmore.common.base.BaseFragment
import com.loadmore.common.base.RVBaseHolder
import com.loadmore.common.constants.Api
import com.loadmore.common.model.BaseResponse
import com.loadmore.common.model.Plan
import com.loadmore.common.net.Request
import com.loadmore.common.net.impl.JsonCallback
import com.loadmore.common.router.RouterPath
import com.loadmore.common.variables.Variables
import com.loadmore.oneday.R
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_message.*

class PlanFragment:BaseFragment() {
    lateinit var adapter:BaseAdapter
    companion object {
        public fun newInstance(bundle: Bundle):PlanFragment {
            val planFragment = PlanFragment()
            planFragment.arguments = bundle
            return planFragment
        }

    }
    override fun getLayoutId(): Int {
        return R.layout.fragment_message
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        actionBarTitle("计划")
        setRefreshEnable(true)
        listview.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        adapter =  object :BaseAdapter(context){
            override fun getLayoutId(): Int {
                return R.layout.layout_plan_item
            }

            override fun onBindViewHolder(holder: RVBaseHolder<*>, position: Int) {
                val plan = getItem(position) as Plan
                holder.setText(R.id.title,plan.title).setText(R.id.description,plan.description).setText(R.id.plan_time,plan.start_time+plan.end_time)
                holder.itemView.setOnClickListener {
                    var bundle = Bundle()
                    bundle.putString("module",RouterPath.PlanDetail)
                    bundle.putSerializable("data",plan)
                    ARouter.getInstance().build(RouterPath.Module).with(bundle).navigation()
                }
            }

        }

        listview.adapter = adapter
        getPlans(false)
        refreshLayout.setOnRefreshListener {
            getPlans(true)
        }
    }

    private fun getPlans(refresh:Boolean) {
        Request.get(Api.plan, hashMapOf<String,Any>("author_id" to Variables.user?.id.toString()),object :JsonCallback<BaseResponse<List<Plan>>>() {
            override fun onSuccess(response: Response<BaseResponse<List<Plan>>>?) {
                val data = response?.body()?.data
                if (refresh)
                    adapter.clear()
                if (data != null && data.isNotEmpty()) {
                    adapter.append(data)
                }
            }

            override fun onError(response: Response<BaseResponse<List<Plan>>>?) {
                super.onError(response)

            }

        })
    }

    override fun retry() {
        super.retry()
    }

}